package com.example.building_material_mannager.controller;

import com.example.building_material_mannager.entity.Product;
import com.example.building_material_mannager.entity.Protype;
import com.example.building_material_mannager.entity.Supplier;
import com.example.building_material_mannager.service.ProductService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;
@RequestMapping("/product")
@Controller
public class ProductController {
    @Resource
    ProductService productService;
    //展示
    @ResponseBody
    @RequestMapping("/show")
    public List<Product> showProduct(Integer pid,String pname,Integer ptid,Integer sid ){
        //创建product对象
        Product product=new Product();
        if (pid!=null){
            product.setPid(pid);
        }
        if (pname!=null){
            product.setPname(pname);
        }
        if(ptid!=null){
            product.setProtype(new Protype(ptid,null));
        }
        if(sid!=null){
            product.setSupplier(new Supplier(sid,null,null,null,null));
        }


        List<Product> products = productService.showProduct(product);
        return products;

    }

    //增加
    @ResponseBody
    @RequestMapping("/add")
    public boolean addProduct(Product product){
        System.out.println(product);
        boolean re = productService.addProduct(product);
        return re;
    }
    //刪除
    @ResponseBody
    @RequestMapping("/del")
    public boolean delProduct(Integer pid){
        boolean re = productService.delProduct(pid);
       return re;
    }
    //更新
    @ResponseBody
    @RequestMapping("/up")
    public boolean upProduct(Product product){
        System.out.println(product);
        boolean re = productService.upProduct(product);
        return re;
    }
}
